


export function FormatViewTypeString(str) {
    if (str.indexOf(':') != -1) {
        var strArray = str.split(':') || [];
        return {
            ViewType: strArray[0],
            Key: strArray[1]
        }
    } else {
        return {
            ViewType: null,
            Key: str
        }
    }
}


// 执行 基于对象的字符串方法
export const StringToFun = (string, Object) => {
    var keyArray = [];
    var ValueArray = [];
    for (const key in Object) {
        keyArray.push(key);
        ValueArray.push(Object[key]);
    };
    try {
        return new Function(keyArray.join(','), 'return ' + string).apply(null, ValueArray);
    } catch (error) {
        console.error('StringToFun => Rule 字段配置错误', string, error);
    }
}


// 从二进制值 到 Select Value
export const FormatIsbyteSelectValue = (val, dict) => {
    if (val === null || val === undefined || dict.length == 0) {
        return []
    } else {
        var res = [];
        for (let i = 0; i < dict.length; i++) {
            const element = dict[i];
            if ((val & element.value) === element.value) {
                res.push(element.value);
            }
        }
        return res;
    }
}


// 从 Select 到 二进制值
export const FormatIsbyteFormValue = (val) => {
    var res = 0;
    for (let i = 0; i < val.length; i++) {
        const element = val[i];
        res = res | element;
    }
    return res;
}



// 根据配置，格式化字典项
export const FormatDictWithOption = (Dict, Option) => {
    if (!Option) {
        return Dict
    }
    var res = [];
    for (let i = 0; i < Dict.length; i++) {
        const element = Dict[i];
        res.push({
            value: element[Option.valueKey],
            label: element[Option.labelKey],
        });
    }
    return res;
}